/** 
 * @file ppw
 * @brief PPW's Dilu_C Mind+ library.
 * @n This is a MindPlus graphics programming extension for PPW's Dilu intelligent vehicle.
 * 
 * @copyright    PPW,2024.06
 * @copyright    MIT Lesser General Public License
 * 
 * @author [email](info@pinpinwa.cn)
 * @date  2024-06-24
*/

//% color="#ef5b9c" iconWith=50 iconHeight=40
namespace dilu_c{


    //--------- 以下为电机控制代码块 ---------//
    //% block="DiLu [RUN] Speed [SPEED]" blockType="command"
    //% RUN.shadow="dropdown" RUN.options="RUN"
    //% SPEED.shadow="range" SPEED.params.min=0 SPEED.params.max=255 SPEED.defl=100
    export function Dilu_Run(parameter:any, block:any){
        let run = parameter.RUN.code;
        let speed = parameter.SPEED.code;

        Generator.addInclude("includeDilu", "#include <Dilu_C.h>", true);
        Generator.addObject(`calssDilu`, `Dilu_C`, `myDilu_C`,true);

        Generator.addCode(`myDilu_C.diluRun(${run},${speed});`);

    }

    //% block="[MOTOR] Motor Run Speed [SPEED] Dir [DIR]" blockType="command"
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR"
    //% SPEED.shadow="range" SPEED.params.min=0 SPEED.params.max=255 SPEED.defl=100
    //% DIR.shadow="dropdown" DIR.options="DIR"
    export function Dilu_MotorRun(parameter:any, block:any){
        let motor = parameter.MOTOR.code;
        let speed = parameter.SPEED.code;
        let dir = parameter.DIR.code;

        Generator.addInclude("includeDilu", "#include <Dilu_C.h>", true);
        Generator.addObject(`calssDilu`, `Dilu_C`, `myDilu_C`,true);

        Generator.addCode(`myDilu_C.motorRun(${motor}, ${dir}, ${speed});`);


    }

    //% block="[MOTOR] Motor Stop" blockType="command"
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR"
    export function Dilu_MotorStop(parameter:any, block:any){
        let motor = parameter.MOTOR.code;

        Generator.addInclude("includeDilu", "#include <Dilu_C.h>", true);
        Generator.addObject(`calssDilu`, `Dilu_C`, `myDilu_C`,true);

        Generator.addCode(`myDilu_C.motorStop(${motor});`)
    }

    //--------- 以下为车头灯控制模块代码块 ---------//
    //% block="DiLu [LIGHT] LED [TOGGLE]" blockType="command"
    //% TOGGLE.shadow="dropdown" TOGGLE.options="TOGGLE"
    //% LIGHT.shadow="dropdown" LIGHT.options="LIGHT"
    export function Dilu_toggleLED(parameter:any, block:any){
        let toggle = parameter.TOGGLE.code;  
        let light = parameter.LIGHT.code;

        Generator.addInclude("includeDilu", "#include <Dilu_C.h>", true);
        Generator.addObject(`calssDilu`, `Dilu_C`, `myDilu_C`,true);

        Generator.addCode(`myDilu_C.toggleLED(${light},${toggle});`);
    }

    //--------- 以下为舵机控制代码块 ---------//
    //% block="Servo [SERVO] Angle [VALUE]" blockType="command"
    //% SERVO.shadow="dropdown" SERVO.options="SERVO"
    //% VALUE.shadow="angle" VALUE.params.edge=1
    export function setServoAngle(parameter:any, block:any){
        let servo = parameter.SERVO.code;
        let value = parameter.VALUE.code;

        Generator.addInclude("includeDilu", "#include <Dilu_C.h>", true);
        Generator.addObject(`calssDilu`, `Dilu_C`, `myDilu_C`,true);
     
        Generator.addCode(`myDilu_C.setServoAngle(${servo},${value});`);
    }

    //--------- 以下为巡线传感器代码块 ---------//
    //% block="Line Tracking Sensor Status [STATUS]" blockType="boolean"
    //% STATUS.shadow="dropdown" STATUS.options="STATUS"
    export function Dilu_Status(parameter:any, block:any){
        let status = parameter.STATUS.code;

        Generator.addInclude("includeDilu", "#include <Dilu_C.h>", true);
        Generator.addObject(`calssDilu`, `Dilu_C`, `myDilu_C`,true);

        Generator.addCode(`myDilu_C.isIrStatus(${status})`);

    }

    //--------- 以下为返回3路巡线传感器值代码块 ---------//
    //% block="Get Tracking Sensor Value" blockType="reporter"
    export function Dilu_Get_tsValue(parameter:any, block:any){
        
        Generator.addInclude("includeDilu", "#include <Dilu_C.h>", true);
        Generator.addObject(`calssDilu`, `Dilu_C`, `myDilu_C`,true);

        Generator.addCode(`myDilu_C.getIrStatus()`);
    }

    //--------- 以下为 C32 超声波测距模块代码块 ---------//
    //% block="Read Ultrasonic Distance (MM)" blockType="reporter"
    export function Dilu_readUltrasonicDistance(parameter:any, block:any){
        Generator.addInclude("includeDilu", "#include <Dilu_C.h>", true);
        Generator.addObject(`calssDilu`, `Dilu_C`, `myDilu_C`,true);

        Generator.addCode(`myDilu_C.e2t1UltraDis()`);
    }

    //--------- 以下为幻彩氛围灯代码块 ---------//
    //% block="RGB [LED_START]to[LED_END] Show [COLOR_PALETTE]" blockType="command"
    //% LED_START.shadow="range" LED_START.params.min=1 LED_START.params.max=4 LED_START.defl=1
    //% LED_END.shadow="range" LED_END.params.min=1 LED_END.params.max=4 LED_END.defl=1
    //% COLOR_PALETTE.shadow="colorPalette" 
    export function setRangeColor(parameter:any, block:any){
        let led_start = parameter.LED_START.code;
        let led_end = parameter.LED_END.code;
        let color_palette = parameter.COLOR_PALETTE.code;

        Generator.addInclude("includeNeoPixel", "#include <DFRobot_NeoPixel.h>", true);
        Generator.addObject(`calssNeoPixel`, `DFRobot_NeoPixel`, `myNeoPixel`,true);
        Generator.addSetup(`iniNeoPixel`, `myNeoPixel.begin(P15, 4, 255);`,true);

        Generator.addCode(`myNeoPixel.setRangeColor(${led_start-1}, ${led_end-1}, ${color_palette});`);
    }

    //% block="Clear All RGB LEDs" blockType="command"
    export function clearAllRGB(parameter:any, block:any){

        Generator.addInclude("includeNeoPixel", "#include <DFRobot_NeoPixel.h>", true);
        Generator.addObject(`calssNeoPixel`, `DFRobot_NeoPixel`, `myNeoPixel`,true);
        Generator.addSetup(`iniNeoPixel`, `myNeoPixel.begin(P15, 4, 255);`,true);

        Generator.addCode(`myNeoPixel.clear();`);
    }

    //% block="Color Red[RVALUE] Green[GVALUE] Blue[BVALUE]" blockType="reporter"
    //% RVALUE.shadow="range" RVALUE.params.min=0 RVALUE.params.max=255 RVALUE.defl=255
    //% GVALUE.shadow="range" GVALUE.params.min=0 GVALUE.params.max=255 GVALUE.defl=255
    //% BVALUE.shadow="range" BVALUE.params.min=0 BVALUE.params.max=255 BVALUE.defl=255
    export function DiLu_RGB_VALUE(parameter:any, block:any){
        let rvalue = parameter.RVALUE.code;
        let gvalue = parameter.GVALUE.code;
        let bvalue = parameter.BVALUE.code;

        Generator.addCode(`(${rvalue},${gvalue},${bvalue})`); 
    }

    //--------- 以下为红外遥控接收代码块 ---------//
    let irpin = `P16`;
    let irObject = `IrReceiver`;

    //% block="IR Data Availabe?" blockType=boolean
    export function isIrData(parameter:any, block:any){
        Generator.addInclude("includeIrReceiver", `#include <IRremote.h>\n#define DECODE_NEC\n#define RECV_PIN P16`,true);
        Generator.addSetup(`${irObject}.enableIRIn`, `${irObject}.begin(${irpin});`,true);
        Generator.addCode(`${irObject}.decode()`);
        
    }

    //% block="Received IR Data" blockType=reporter
    export function readIrData(parameter:any, block:any){

        Generator.addInclude("includeIrReceiver", `#include <IRremote.h>\n#define DECODE_NEC\n#define RECV_PIN P16`,true);
        Generator.addSetup(`${irObject}.enableIRIn`, `${irObject}.begin(${irpin});`,true);
        

        Generator.addCode(`${irObject}.decodedIRData.command`)
        
        
    }
    
    //% block="Resume Read IR Data" blockType="command"
    export function readIRResume(parameter: any, block: any) {
        Generator.addInclude("includeIrReceiver", `#include <IRremote.h>\n#define DECODE_NEC\n#define RECV_PIN P16`,true);
        Generator.addSetup(`${irObject}.enableIRIn`, `${irObject}.begin(${irpin});`,true);
        Generator.addCode(`${irObject}.resume();`);
    }

    //% block="IR Remote [KEY] Value" blockType="reporter"
    //% KEY.shadow="dropdown" KEY.options="20KEY_VALUE"
    export function value20Key(parameter:any, block:any){
        let value_key = parameter.KEY.code;
        Generator.addCode(`${value_key}`);
    }
      

}